package al.ui;



	import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import org.jdesktop.swingx.JXFrame;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.AbstractMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;

import al.jdbc.DAO.Gestion_courrierDAO;
import al.jdbc.DAO.OrganigrammeDAO;
import al.jdbc.dpbridge.MySqlDataSource;
import alOmrane.jdbc.beans.Organisme;

	@SuppressWarnings("serial")
	public class Organigramme extends JPanel implements MouseListener{
		
		JPanel contentB;
		//JPanel contentR;
		//Button bp1;
		Button bp;
		//Form f;
		public int nbrCols;
		private JScrollPane scroll;
		private JButton validate=new JButton("Valider");
		private JXTreeTable tree;
		private DefaultTreeTableModel mdl;
		private final String[] headings = { "Organigramme"};
		private OrganigrammeDAO cdao = Gestion_courrierDAO.getInstance().getOrganigrammeDao();
		private int nivCount = 0;
		
		
		public Organigramme(){
			this.setLayout(new BorderLayout());
			this.setBackground(Color.WHITE);
			
			contentB = new JPanel();
			//contentB.setLayout(new BorderLayout());
			contentB.setBackground(Color.WHITE);
			nivCount = cdao.getNivCount();
			OrganigrammeNode n1 = new OrganigrammeNode(cdao.findAll().get(0));
			/*n1.add(new Node(new Object[]{cdao.findAll().get(1)}));
			n1.add(new Node(new Object[]{cdao.findAll().get(2)}));
			n1.setUserObject(new Object[]{cdao.findAll().get(3)});*/
			
			loadData(n1);
			
			mdl = new DefaultTreeTableModel(n1, Arrays.asList(headings));
			tree = new JXTreeTable(mdl);
			scroll = new JScrollPane(tree);
			
			tree.setShowGrid(true, true);
			tree.setRootVisible(true);
			tree.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tree.setColumnControlVisible(true);
			tree.setHorizontalScrollEnabled(true);
			tree.setFillsViewportHeight(false);
			tree.expandRow(0);
			tree.packAll();
			
			
			
			GroupLayout layout = new GroupLayout(contentB);
			layout.setHorizontalGroup(
		            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		            .addGroup(layout.createSequentialGroup()
		                .addContainerGap()
		                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		                    .addComponent(scroll, javax.swing.GroupLayout.DEFAULT_SIZE, 692, Short.MAX_VALUE)
		                    .addGroup(layout.createSequentialGroup()
		                        .addGap(0, 0, Short.MAX_VALUE)
		                        .addComponent(validate, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
		                .addContainerGap())
		        );
		        layout.setVerticalGroup(
		            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		            .addGroup(layout.createSequentialGroup()
		                .addContainerGap()
		                .addComponent(scroll, javax.swing.GroupLayout.PREFERRED_SIZE, 450, javax.swing.GroupLayout.PREFERRED_SIZE)
		                .addGap(18, 18, 18)
		                .addComponent(validate)
		                .addContainerGap(16, Short.MAX_VALUE))
		        );
			
			contentB.setLayout(layout);	

			//content.add(contentB);
			
			
			
									
			//this.add("North",contentB);
			this.add("Center",contentB);
		}
	


		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		private class Node extends AbstractMutableTreeTableNode{
			public Node(Object [] data) {
				super(data);
			}
			@Override
			public int getColumnCount() {
				return getData().length;
			}
			@Override
			public Object getValueAt(int column) {
				return getData()[column];
			}

			public Object[] getData() {
				return (Object[]) super.getUserObject();
			}
		}
		private class OrganigrammeNode extends Node{

			public OrganigrammeNode(alOmrane.jdbc.beans.Organigramme data) {
				super(new Object[]{data});
			}
			
			public List<OrganigrammeNode> getChilds(){
				if(cdao.hasChilds((alOmrane.jdbc.beans.Organigramme)super.getData()[0])){
					List<alOmrane.jdbc.beans.Organigramme> orgs = cdao.getChilds((alOmrane.jdbc.beans.Organigramme)super.getData()[0]);
					List<OrganigrammeNode> childs = new ArrayList<OrganigrammeNode> ();
					if(orgs != null ) {
						for(int i=0;i<orgs.size();i++)
						childs.add(new OrganigrammeNode(orgs.get(i)));
						return childs;
					}else return null;
				}
				return null;
			}
			@Override
			public String toString() {
				// TODO Auto-generated method stub
				return ((alOmrane.jdbc.beans.Organigramme)super.getData()[0]).toString();
			}
		}
		public static void main(String[] args) {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						JXFrame frame = new JXFrame();
						Organigramme fc = new Organigramme();
						fc.setVisible(true);
						frame.getContentPane().add(fc);
						frame.pack();
						frame.setVisible(true);
						frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		public void loadData(OrganigrammeNode on){
			List<OrganigrammeNode> ons = on.getChilds();
			if(ons != null){
				for(int i=0;i<ons.size();i++){
					on.add(ons.get(i));
				}
				for(int i=0;i<ons.size();i++){
					loadData(ons.get(i));
				}
			}
		}
}
	
